package com.qf.java2107.yb.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2107.yb.hotel.common.ResultVO;
import com.qf.java2107.yb.hotel.dao.TableDao;
import com.qf.java2107.yb.hotel.entity.BeanFactory;
import com.qf.java2107.yb.hotel.pojo.Table;
import com.qf.java2107.yb.hotel.service.TableService;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author YB
 * @date 2021/12/16
 * @VERSION V1.0
 **/
public class TableServiceImpl implements TableService {

    private TableDao tableDao = (TableDao) BeanFactory.getBean("tableDao");


    @Override
    public ResultVO findLikeName(String tableName) {
        try {
            if(StringUtils.isEmpty(tableName)){
                tableName="%%";
            }else{
                tableName = "%"+tableName.trim()+"%";
            }
            List<Table> list = tableDao.findLikeName(tableName);
            return ResultVO.ok("查询成功",list);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error("查询失败",e.getMessage());
        }
    }

    @Override
    public ResultVO save(Table table) {
        try {
            table.setTableStatus(0);
            table.setReserVationTime(new Date());
            int save = tableDao.save(table);
            return ResultVO.ok("添加成功");
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error("添加失败");
        }
    }

    @Override
    public ResultVO update(Table table) {
        try {
            if(table.getTableStatus()==0){
                table.setTableStatus(1);
            }else if(table.getTableStatus()==1){
                table.setTableStatus(0);
            }
            int update = tableDao.update(table);
            if(update==1){
                return ResultVO.ok("更新成功");
            }
            return ResultVO.error("失败");
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error("更新失败");
        }
    }

    @Override
    public ResultVO delete(Integer tableId) {

        try {
            int i = tableDao.delete(tableId);
            if(i==1){
                return ResultVO.ok("删除成功");
            }return ResultVO.error("删除失败");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ResultVO.error("删除失败");
    }
}
